package ms.jasim.console.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SpringLayout;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import ms.jacrim.framework.Configuration;
import ms.jasim.console.gui.component.DialogHeaderPanel;
import ms.jasim.console.gui.component.MP_EventPanel;
import ms.jasim.console.gui.component.MP_PlannerPanel;
import ms.jasim.console.gui.component.MP_SolutionPanel;
import ms.jasim.framework.DefaultEventTypeProvider;
import ms.jasim.framework.PddlModel;
import ms.spm.IAppContext;
import ms.spm.implement.SimpleAppContext;

public class ModelPlanner extends JFrame {
	private MP_EventPanel eventPanel;
	private SpringLayout springLayout;
	private MP_SolutionPanel solutionPanel;
	private MP_PlannerPanel plannerPanel;
	private static final long serialVersionUID = 1L;
	public IAppContext Context;

	/*
	 * Launch the application
	 * 
	 * @param args
	 */
	public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
			UnsupportedLookAndFeelException {
		// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IAppContext context = SimpleAppContext.createRootContext();
					Configuration config = context.getService(Configuration.class);
					String modelFile = config.get("model_file");
					PddlModel model = new PddlModel();
					model.loadFromFile(modelFile);
					if (model != null)
						context.addService(model);
					context.addService(DefaultEventTypeProvider.createStdEventSet());
					
					ModelPlanner frame = new ModelPlanner();
					frame.setContext(context);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/** * Create the frame */
	public ModelPlanner() {
		super();
		springLayout = new SpringLayout();
		getContentPane().setLayout(springLayout);
		setBounds(100, 100, 790, 539);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//		
		final DialogHeaderPanel dialogHeaderPanel = new DialogHeaderPanel();
		dialogHeaderPanel.setSubTitle("This dialog allows you to modify the parameters sent to the solution planner.");
		dialogHeaderPanel.setTitle("Model Planner");
		getContentPane().add(dialogHeaderPanel);
		springLayout.putConstraint(SpringLayout.EAST, dialogHeaderPanel, 0, SpringLayout.EAST, getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, dialogHeaderPanel, 0, SpringLayout.WEST, getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, dialogHeaderPanel, 65, SpringLayout.NORTH, getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, dialogHeaderPanel, 0, SpringLayout.NORTH, getContentPane());
		final JPanel panel = new JPanel();
		final FlowLayout flowLayout = new FlowLayout();
		flowLayout.setVgap(9);
		flowLayout.setAlignment(FlowLayout.TRAILING);
		panel.setLayout(flowLayout);
		getContentPane().add(panel, BorderLayout.SOUTH);
		springLayout.putConstraint(SpringLayout.SOUTH, panel, -5, SpringLayout.SOUTH, getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, panel, -50, SpringLayout.SOUTH, getContentPane());
		final JButton okButton = new JButton();
		okButton.setMargin(new Insets(2, 26, 2, 26));
		okButton.setMinimumSize(new Dimension(100, 0));
		okButton.setText("OK");
		panel.add(okButton);
		final JButton cancelButton = new JButton();
		cancelButton.setText("Cancel");
		panel.add(cancelButton);
		final JTabbedPane tabbedPane = new JTabbedPane();
		getContentPane().add(tabbedPane, BorderLayout.CENTER);
		springLayout.putConstraint(SpringLayout.EAST, tabbedPane, 0, SpringLayout.EAST, dialogHeaderPanel);
		springLayout.putConstraint(SpringLayout.WEST, tabbedPane, 0, SpringLayout.WEST, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, panel, 0, SpringLayout.EAST, tabbedPane);
		springLayout.putConstraint(SpringLayout.WEST, panel, 0, SpringLayout.WEST, tabbedPane);
		springLayout.putConstraint(SpringLayout.SOUTH, tabbedPane, -55, SpringLayout.SOUTH, getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, tabbedPane, 0, SpringLayout.SOUTH, dialogHeaderPanel);
		plannerPanel = new MP_PlannerPanel();
		tabbedPane.addTab("Planner", null, plannerPanel, null);
		solutionPanel = new MP_SolutionPanel();
		tabbedPane.addTab("Solutions", null, solutionPanel, null);
		eventPanel = new MP_EventPanel();
		tabbedPane.addTab("Event Simulation", null, eventPanel, null);
	}

	public void setContext(IAppContext context) {
		Context = context.createChildContext("ModelPlanner");
		
		Context.addService(plannerPanel);
		Context.addService(solutionPanel);
		Context.addService(eventPanel);
		
		plannerPanel.setContext(Context);
		solutionPanel.setupContext(Context);
		eventPanel.setupContext(Context);
		
		solutionPanel.setupListener(plannerPanel);
	}

	@Override
	public void dispose() {
		eventPanel.dispose();
		super.dispose();
	}
}